var e = require;
var t = module;
var o = exports;
var n,
    i = o,
    a =
        ((n = function (e, t) {
            return (n =
                Object.setPrototypeOf ||
                ({__proto__: []} instanceof Array &&
                    function (e, t) {
                        e.__proto__ = t;
                    }) ||
                function (e, t) {
                    for (var o in t) t.hasOwnProperty(o) && (e[o] = t[o]);
                })(e, t);
        }),
        function (e, t) {
            function o() {
                this.constructor = e;
            }
            n(e, t), (e.prototype = null === t ? Object.create(t) : ((o.prototype = t.prototype), new o()));
        }),
    r = function (e, t, o, n) {
        var i,
            a = arguments.length,
            r = a < 3 ? t : null === n ? (n = Object.getOwnPropertyDescriptor(t, o)) : n;
        if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) r = Reflect.decorate(e, t, o, n);
        else
            for (var s = e.length - 1; s >= 0; s--)
                (i = e[s]) && (r = (a < 3 ? i(r) : a > 3 ? i(t, o, r) : i(t, o)) || r);
        return a > 3 && r && Object.defineProperty(t, o, r), r;
    };
Object.defineProperty(i, "__esModule", {value: !0});
var s = cc._decorator,
    c = s.ccclass,
    l = s.property,
    d = (function (e) {
        function t() {
            var t = (null !== e && e.apply(this, arguments)) || this;
            return (
                (t.spacingX = 100),
                (t.itemWidth = 150),
                (t.content = null),
                (t.items = []),
                (t.currentSelectIndex = -1),
                (t.isScrolling = !1),
                (t.skipUpdateFun = null),
                (t.skipUpdateIndexFun = null),
                t
            );
        }
        return (
            a(t, e),
            (t.prototype.init = function () {
                var e = this.node.getChildByName("view");
                e.on(cc.Node.EventType.TOUCH_MOVE, this.scrollviewEvent, this),
                    (this.content = e.getChildByName("content")),
                    this.clearData();
            }),
            (t.prototype.clearData = function (e) {
                void 0 === e && (e = !0),
                    this.content.destroyAllChildren(),
                    e &&
                        ((this.content.position = cc.Vec3.ZERO),
                        (this.content.x -= this.itemWidth / 2),
                        (this.content.height = 0)),
                    (this.currentSelectIndex = e ? 0 : Math.max(0, this.currentSelectIndex)),
                    (this.isScrolling = !1),
                    (this.items.length = 0);
            }),
            (t.prototype.addPage = function (e) {
                if (this.content) {
                    (e.parent = null), this.content.addChild(e);
                    var t = this.items.push(e);
                    (this.content.height += this.itemWidth + this.spacingX),
                        (e.scale = t - 1 === this.currentSelectIndex ? 1 : 0.8),
                        (e.x = 0);
                } else game.warn("请先执行init()方法");
            }),
            (t.prototype.registerSkipEvent = function (e, t) {
                e && (this.skipUpdateFun = e.bind(t));
            }),
            (t.prototype.registerIndexSkipEvent = function (e, t) {
                e && (this.skipUpdateIndexFun = e.bind(t));
            }),
            (t.prototype.itemCount = function () {
                return this.items.length;
            }),
            (t.prototype.clickItemIndex = function (e) {
                var t = this;
                if (((e = Math.min(e, this.items.length - 1)), (e = Math.max(e, 0)) !== this.currentSelectIndex)) {
                    var o = e < this.currentSelectIndex ? 0 : 1;
                    this.scrollProccessor(o, function () {
                        t.judgeSkipOut(e);
                    });
                }
            }),
            (t.prototype.getCurrentIndex = function () {
                return this.currentSelectIndex;
            }),
            (t.prototype.getCurrentIndexItem = function () {
                return this.items[this.currentSelectIndex];
            }),
            (t.prototype.scrollviewEvent = function (e) {
                var t = e.getDelta().x;
                t > 10 ? this.scrollProccessor(0) : t < -10 && this.scrollProccessor(1);
            }),
            (t.prototype.scrollProccessor = function (e, t) {
                var o = this;
                if ((void 0 === t && (t = null), !this.isScrolling || t)) {
                    var n = this.itemWidth + this.spacingX;
                    0 === e && this.currentSelectIndex > 0
                        ? ((this.isScrolling = !0),
                          --this.currentSelectIndex,
                          this.skipUpdateFun &&
                              this.skipUpdateFun(
                                  this.items[this.currentSelectIndex + 1],
                                  this.items[this.currentSelectIndex]
                              ),
                          this.skipUpdateIndexFun &&
                              this.skipUpdateIndexFun(this.currentSelectIndex + 1, this.currentSelectIndex),
                          cc.tween(this.items[this.currentSelectIndex]).to(0.2, {scale: 1}).start(),
                          cc
                              .tween(this.items[this.currentSelectIndex + 1])
                              .to(0.2, {scale: 0.8})
                              .start(),
                          t
                              ? cc
                                    .tween(this.content)
                                    .by(0.3, {x: n})
                                    .call(function () {
                                        t ? t() : (o.isScrolling = !1);
                                    })
                                    .start()
                              : cc
                                    .tween(this.content)
                                    .by(0.3, {x: n}, {easing: "quadIn"})
                                    .call(function () {
                                        t ? t() : (o.isScrolling = !1);
                                    })
                                    .start())
                        : 1 === e &&
                          this.currentSelectIndex < this.items.length - 1 &&
                          ((this.isScrolling = !0),
                          ++this.currentSelectIndex,
                          this.skipUpdateFun &&
                              this.skipUpdateFun(
                                  this.items[this.currentSelectIndex - 1],
                                  this.items[this.currentSelectIndex]
                              ),
                          this.skipUpdateIndexFun &&
                              this.skipUpdateIndexFun(this.currentSelectIndex - 1, this.currentSelectIndex),
                          cc.tween(this.items[this.currentSelectIndex]).to(0.2, {scale: 1}).start(),
                          cc
                              .tween(this.items[this.currentSelectIndex - 1])
                              .to(0.2, {scale: 0.8})
                              .start(),
                          t
                              ? cc
                                    .tween(this.content)
                                    .by(0.3, {x: -n})
                                    .call(function () {
                                        t ? t() : (o.isScrolling = !1);
                                    })
                                    .start()
                              : cc
                                    .tween(this.content)
                                    .by(0.3, {x: -n}, {easing: "quadIn"})
                                    .call(function () {
                                        t ? t() : (o.isScrolling = !1);
                                    })
                                    .start());
                }
            }),
            (t.prototype.judgeSkipOut = function (e) {
                e === this.currentSelectIndex ? (this.isScrolling = !1) : this.clickItemIndex(e);
            }),
            (t.prototype.left = function () {
                0 === this.currentSelectIndex || this.isScrolling || this.judgeSkipOut(this.currentSelectIndex - 1);
            }),
            (t.prototype.right = function () {
                this.currentSelectIndex === this.items.length - 1 ||
                    this.isScrolling ||
                    this.judgeSkipOut(this.currentSelectIndex + 1);
            }),
            r([l], t.prototype, "spacingX", void 0),
            r([l], t.prototype, "itemWidth", void 0),
            r([c], t)
        );
    })(cc.Component);
i.default = d;
